package com.qingluan.dd.InetTool;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import com.qingluan.dd.InetTool.DATA.*;
public class ParaJson  {
	private byte[] dataByte ;	
	private String PrimaryJsonObject;
	private JSONObject jsonObj ;
	private Map<String,Integer> TYPE_MAP  = new HashMap<String,Integer>();
	private Integer TYPE;
	
	public ParaJson(byte[] buffer){
		TYPE_MAP.put("mission", 0);
		TYPE_MAP.put("person",1);
		TYPE_MAP.put("location", 2);
		this.dataByte = buffer;
		try {
			this.decode();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public ParaJson(String jsonObject){
		TYPE_MAP.put("mission", 0);
		TYPE_MAP.put("person",1);
		TYPE_MAP.put("location", 2);
		this.PrimaryJsonObject = jsonObject;
	}
	
	/**
	 *  this could resolve Json from bytes directly;
	 * @return  if decode
	 * @throws UnsupportedEncodingException
	 */
	public boolean  decode() throws UnsupportedEncodingException{
		if (this.dataByte.length > 0){
			this.PrimaryJsonObject =new String(this.dataByte,0,this.dataByte.length, "utf-8");
			return true;
		}
		return false;
	}
	
	public void Indentify() throws JSONException{
		jsonObj = new JSONObject(this.PrimaryJsonObject);
		String tem =  (String) jsonObj.get("type");
		this.TYPE = this.TYPE_MAP.get(tem);	
		
	}
	
	/*
	 * 
	 * 
	 */
	public String get(String key) throws JSONException{
		return this.jsonObj.getString(key);
	}
	
}
